<template>
  <div class="channel-edit">
    <van-cell :border="false">
        <template #title>
            <div class="title-text">我的频道</div>
        </template>
        <van-button class="edit-btn" @click="isEditShow = !isEditShow"
            size="mini" round type="danger" plain
        >{{ isEditShow ? '完成' : '编辑'}}
        </van-button>
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
        <van-grid-item class="channel-item" @click="onMyChannelClick(channel, index)"
        v-for="(channel, index) in myChannels" :key="index">

        <!-- 图标插槽 -->
        <template #icon>
            <van-icon  name="close" v-show="isEditShow && index !== 0"></van-icon>
        </template>
        <!-- /图标插槽 -->

        <template #text>
          <span class="text" :class="{active: active === index}">
            {{ channel.name }}</span>
        </template>

        </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
     <van-cell :border="false">
        <template #title>
            <div class="title-text">频道推荐</div>
        </template>
    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
        <van-grid-item class="channel-item" icon="plus"
        v-for="(channel, index) in recommendChannels" :key="index" :text="channel.name" @click="onAddChannel(channel)"/>
    </van-grid>
  </div>
</template>

<script>
import { getAllChannelsAPI, addUserChannelAPI, deleteUserChannelAPI } from '@/api/index.js'
import { mapState } from 'vuex'
import { setItem } from '../../../utils/storage.js'
export default {
  name: 'ChannelEdit',
  props: {
    myChannels: {
      type: Array,
      default: () => []
    },
    active: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      allChannels: [], // 所有频道
      myChannelsClone: this.myChannels,
      isEditShow: false // 控制编辑状态的显示
    }
  },
  computed: {
    ...mapState(['user']),
    // 计算属性会观测内部依赖数据的变化
    // 如果依赖的数据发生变化，则计算属性会重新执行
    recommendChannels () {
      // 数组的 filter 方法：遍历数组，把符合条件的元素存储到新数组中并返回
      return [...this.$_.differenceBy(this.allChannels, this.myChannels, 'id')]
    }
  },
  created () {
    // 获取所有频道
    this.loadAllChannels()
  },
  methods: {
    async loadAllChannels () {
      try {
        const { data } = await getAllChannelsAPI()
        this.allChannels = data.data.channels
        // console.log(data)
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    // 添加频道
    async onAddChannel (channel) {
      // 不可以直接修改父组件传递过来的参数，所以拷贝一份在data中
      this.myChannelsClone.push(channel)
      if (this.user) {
        // 已登录
        try {
          // 已登录，数据存储到线上
          await addUserChannelAPI({
            id: channel.id, // 频道 id
            seq: this.myChannels.length // 频道的 序号
          })
          this.$toast('添加成功')
        } catch (err) {
          this.$toast('保存失败')
        }
      } else {
        // 未登陆 把数据存储到本地
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },

    // 频道功能方法
    onMyChannelClick (channel, index) {
      if (this.isEditShow) {
        // 1. 编辑状态删除频道
        if (index === 0) return
        this.myChannelsClone.splice(index, 1)
        // 2. 让激活频道的索引 - 1
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        // 处理持久化
        this.deleteChannel(channel)
      } else {
        // 2. 非编辑状态切换频道( 通过isEditShow判断)
        this.$emit('update-active', index)
      }
    },

    // 删除数据持久化
    async deleteChannel (channel) {
      try {
        if (this.user) {
          // 已登录，将数据存储到线上
          await deleteUserChannelAPI(channel.id)
        } else {
          // 未登录，将数据存储到本地
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } catch (error) {
        console.log(error)
        this.$toast('删除频道失败，请稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
    padding: 85px 0;
    .title-text {
    height: 100%;
    line-height: 58px;
    font-size: 32px;
    color: #333333;
  }
  .edit-btn {
    line-height: 48px;
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  .channel-item {
    height: 86px;
    width: 160px;
    :deep(.van-grid-item__content) {
        background-color: #f5f5f6;

        /* 设置按钮字体样式 */
        .van-grid-item__text, .text {
            color: #222;
            font-size: 28px;
            margin-top: 0;
        }
        .active {
            color: red;
        }
        .van-grid-item__icon-wrapper {
            position: unset;
        }
    }
  }
  :deep(.recommend-grid) {
  .van-grid-item__content {
    white-space: nowrap;
    flex-direction: row;

    /* 设置按钮字体样式 */

    .van-icon-plus {
      font-size: 28px;
      margin-right: 6px;
    }
  }
 }
  /deep/ .my-grid {
    .van-grid-item__content {
      .van-icon-close {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }
}
</style>
